package com.jiulan.hearthearing.utils;

import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间相关工具类
 */
public class TimeUtils {

    /**
     * 中文时间格式
     */
    public static String format = "yyyy-MM-dd HH:mm:ss";
    public static String format_date = "yyyy-MM-dd";

    /**
     * 字符串转时间戳
     *
     * @param timeString
     * @return
     */
    public static long getTime(String timeString) {
        if (StringUtils.isEmpty(timeString)) return 0;
        long timeStamp = 0;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date d;
        try {
            d = sdf.parse(timeString);
            timeStamp = d.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }

    public static long getNetTime() {
        String webUrl = "http://www.ntsc.ac.cn";//中国科学院国家授时中心
        try {
            URL url = new URL(webUrl);
            URLConnection uc = url.openConnection();
            uc.setReadTimeout(5000);
            uc.setConnectTimeout(5000);
            uc.connect();
            long correctTime = uc.getDate();
//            Date date = new Date(correctTime);
            return correctTime;
        } catch (Exception e) {
            return System.currentTimeMillis();
        }
    }

    /**
     * 时间戳转字符串
     *
     * @param timeStamp
     * @return
     */
    public static String getStrTime(String timeStamp) {
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }

    public static String getCurrentDate() {
        Calendar calendar = Calendar.getInstance();
        return String.valueOf(calendar.get(Calendar.YEAR) + String.valueOf(calendar.get(Calendar.MONTH) + 1)) + String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * 把Date转化为字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String getDateStr(Date date, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 把字符串转化为Date
     *
     * @param time
     * @param format
     * @return
     */
    public static Date getStringDate(String time, String format) {
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);

        try {
            return formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date();
    }

    public static Date getStringDate(String time) {
        return getStringDate(time, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * yyyy-MM-dd HH:mm:ss转化为yyyy-MM-dd
     *
     * @param dateStr
     * @return
     */
    public static String getMyDate(String dateStr) {
        DateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat s = new SimpleDateFormat(format_date);

        return s.format(date);
    }

    public static List<Long> getTimeDifference(String dateStr) {
        List<Long> list = new ArrayList<>();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime = df.parse(dateStr);
            long diff = startTime.getTime() - System.currentTimeMillis();
            if (diff>0){
                long hours = diff / (1000 * 60 * 60);
                long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60);
                long seconds = (diff - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
                list.add(hours);
                list.add(minutes);
                list.add(seconds);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Long> getTimeDifference(Long dateStr) {
        List<Long> list = new ArrayList<>();
        try {
            long diff = dateStr - System.currentTimeMillis();
            if (diff>0){
                long hours = diff / (1000 * 60 * 60);
                long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60);
                long seconds = (diff - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;
                list.add(hours);
                list.add(minutes);
                list.add(seconds);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年

    /**
     * 返回文字描述的日期
     *
     * @param date
     * @return
     */
    public static String getTimeFormatText(Date date) {
        if (date == null) {
            return null;
        }
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return r + "年前";
        }
        if (diff > month) {
            r = (diff / month);
            return r + "个月前";
        }
        if (diff > day) {
            r = (diff / day);
            return r + "天前";
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        return sb.toString();
    }
}
